#ifndef SCREENCAPTUREVIEW_H
#define SCREENCAPTUREVIEW_H

#include "libs/extension/extensionview.h"
#include <QAudioInput>
#include <QDialog>
#include <QItemSelection>
#include <QMediaRecorder>
#include <QPointer>
#include <QProgressBar>
#include <QScreenCapture>
#include <QSettings>
#include <QShortcut>
#include <QWindowCapture>

QT_BEGIN_NAMESPACE
class QCheckBox;
class QComboBox;
class QLabel;
class QListView;
class QMediaCaptureSession;
class QPushButton;
class QTimer;
class QVideoWidget;
QT_END_NAMESPACE

class ScreenListModel;
class WindowListModel;

// 新增：倒计时对话框类
class CountdownDialog : public QDialog
{
    Q_OBJECT
public:
    explicit CountdownDialog(QWidget* parent = nullptr);
    void setCountdown(int seconds);

private:
    QLabel* countdown_label_;
};

class ScreenCaptureView : public yc::ExtensionView
{
    Q_OBJECT

public:
    explicit ScreenCaptureView(yc::Extension* extension, QWidget* parent = nullptr);
    ~ScreenCaptureView();

    bool isRecording() const { return recording_; }

protected:
    void resizeEvent(QResizeEvent* event) override;
    void showEvent(QShowEvent* event) override;
    void hideEvent(QHideEvent* event) override;

private:
    enum class SourceType { None, Screen, Window };

    void onCurrentScreenSelectionChanged(QItemSelection index);
    void onCurrentWindowSelectionChanged(QItemSelection index);
    void onCaptureError(QMediaRecorder::Error error, const QString& errorString);
    void onScreenCaptureError(QScreenCapture::Error error, const QString& errorString);
    void onWindowCaptureError(QWindowCapture::Error error, const QString& errorString);
    void onRecorderStateChanged(QMediaRecorder::RecorderState state);

    void setupUi();
    void refreshAudioInputsList();
    void updateUi();

    void updateActive(SourceType source_type, bool active);
    void startRecording();
    void doStartRecording();
    void stopRecording();
    void pauseRecording();
    void resumeRecording();
    void updateRecordingInfo();
    void startCountdown();
    void updateCountdown();
    void applyAspectRatio(int index);

    static QString formatTime(qint64 milliseconds);

    // 捕获会话组件
    QMediaCaptureSession* capture_session_;
    QScreenCapture* screen_capture_;
    QWindowCapture* window_capture_;
    QAudioInput* audio_input_;
    QMediaRecorder* media_recorder_;

    // UI 组件
    ScreenListModel* screen_list_model_;
    WindowListModel* window_list_model_;
    QListView* screen_list_view_;
    QListView* window_list_view_;
    QComboBox* audio_source_combo_;
    QPushButton* start_button_;
    QPushButton* stop_button_;
    QPushButton* pause_resume_button_;
    QPushButton* open_location_button_;
    QVideoWidget* preview_widget_;
    QCheckBox* capture_audio_checkbox_;
    QLabel* recording_info_label_;

    // 新增：预览比例适应
    QComboBox* preview_aspect_ratio_combo_;

    // 修改：使用对话框而非标签实现倒计时
    CountdownDialog* countdown_dialog_;
    QTimer* countdown_timer_;
    int countdown_seconds_ = 3;

    // 新增：键盘快捷键
    QShortcut* start_shortcut_;
    QShortcut* stop_shortcut_;
    QShortcut* pause_resume_shortcut_;

    // 状态跟踪
    SourceType source_type_ = SourceType::None;
    bool recording_ = false;
    qint64 recording_start_time_ = 0;
    QTimer* info_update_timer_ = nullptr;
};

#endif // SCREENCAPTUREVIEW_H
